package HomeWork;


import static java.lang.Thread.sleep;

public class YearClass {


    public static void main(String[] args) {
        TestNum testNum=new TestNum();
      //  testNum.suji();
        Thread n1=new Thread(testNum,"前门");
        Thread n2=new Thread(testNum,"后门");

       n1.start();
       n2.start();
    }

}

class TestNum implements Runnable{
    public static int b;
    public static int ID[]=new int[30];
    private int people=1;
    Object object=new Object();
    @Override
    public void run() {

        TestNum testNum=new TestNum();
        testNum.suji();
        while (people<31) {
           synchronized (object) {
                System.out.println(people + "号员工从" + Thread.currentThread().getName()+", 奖牌号为:"+ID[people-1]);
                try {
                   sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               people = people + 1;
            }
        }
    }

    public void suji(){
        while(b < 30) {
            int num = (int) (Math.random() * 30) +1;
            boolean flag = true;
            for (int j = 0; j < b; j++) {
                if(num == ID[j]){
                    flag = false;
                    break;
                }
            }
            if(flag==true){
                ID[b] = num;
                b++;
            }
        }
    }
}


